//
//  KVCustemOriginalView.swift
//  KVBestMicroblog
//
//  Created by 李康卫 on 16/7/23.
//  Copyright © 2016年 李康卫. All rights reserved.
//

import UIKit

class KVCustemOriginalView: UIView {
    //微博模型
    var status: KVStatuses? {
        didSet {
            userNameLabel.text = status?.user?.screen_name
            let creatTime = NSDate.dateFormart((status?.created_at)!)
            let creatString: String = (creatTime?.sinaDateDescription())!
            timeLabel.text = creatString
            sourceLabel.text = status?.source
            contentLabel.text = status?.text
            //设置头像和等级
            loadUserImgInfo()
            pictureView.pictureURLS = status?.pictureURLs
            let count: Int = (pictureView.pictureURLS?.count)!
            //如果没有图片更新底部约束消除间距
            if count == 0{
                self.snp_updateConstraints { (make) -> Void in
                    make.bottom.equalTo(pictureView)
                }
            }
        }
    }
    //图片View
    let pictureView = KVStatusPictureView()

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame:frame)
        self.backgroundColor = UIColor.whiteColor()

//        self.frame.size.height = CGFloat(arc4random_uniform(60) + 40)
        loadCustemSubViews()
    }

    // MARK: - 私有方法
    private func loadUserImgInfo() {
        //头像
        let urlString =  status?.user!.profile_image_url
        HMAsyncDrawImage.sharedAsyncDrawImage.hm_AsyncDrawImage(urlString!) { (image) -> () in
            self.iconView.image = image
        }
//        iconView.sd_setImageWithURL(NSURL(string: urlString!), placeholderImage: UIImage(named: "avatar"))
        /// verified_type 没有认证:-1   认证用户:0  企业认证:2,3,5  达人:220
        let verified_type = (status?.user?.verified_type)!
        if verified_type == -1 {
            avatarView.image = nil
        } else if verified_type == 0 {
            avatarView.image = UIImage(named: "avatar_vip")
        } else {
            switch verified_type {
            case 2,3,5:
                avatarView.image = UIImage(named: "avatar_enterprise_vip")
                break
            case 220:
                avatarView.image = UIImage(named: "breakavatar_grassroot")
            default :
                break
            }
        }
        
        //vipType    /// 会员等级
        let vipTyp = (status?.user?.mbrank)!
        if 0 < vipTyp && vipTyp < 7 {
            let imageName = "common_icon_membership_level\(vipTyp)"
            levelIconView.image = UIImage(named:imageName)
            
        }
    }

    //加载子控件
    private func loadCustemSubViews() {
        
        //设置阴影测试性能
//        iconView.layer.shadowColor = UIColor.cyanColor().CGColor
//        iconView.layer.shadowOffset = CGSize(width: 2, height: 2)
//        iconView.layer.shadowOpacity = 0.5
        
        self.addSubview(iconView)
        addSubview(avatarView)
        addSubview(userNameLabel)
        addSubview(levelIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        //设置子控件的约束
        //头像的约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(homeCellSpace)
            make.width.height.equalTo(homeIconViewHeight)
        }
        //认证
        avatarView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        //昵称
        userNameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(homeCellSpace)
        }
        //vipLevel
        levelIconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(userNameLabel.snp_trailing).offset(homeCellSpace)
            make.centerY.equalTo(userNameLabel)
        }
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(homeCellSpace)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(homeCellSpace*2)
        }
        //content内容
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(homeCellSpace)
            make.trailing.equalTo(self).offset(-homeCellSpace)
            make.top.equalTo(iconView.snp_bottom).offset(homeCellSpace)
        }
        
        //添加pictureView
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(homeCellSpace)
            make.leading.equalTo(self).offset(homeCellSpace)
//            make.trailing.equalTo(self).offset(-homeCellSpace)
            make.size.equalTo(CGSizeMake(300, 300))
        }
        //view的底部大小
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(homeCellSpace)
        }
    }
    
    // MARK: - 加载控制
    //头像
    private lazy var iconView: UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar"))
        return iconView
    }()
    //认证avatar_vip
    private lazy var avatarView: UIImageView = {
        let avatarView = UIImageView(image: UIImage(named: "avatar_vip"))
        return avatarView
    }()
    //呢陈
    private lazy var userNameLabel: UILabel = {
        let userNamelabel = UILabel()
        userNamelabel.text = "titleName"
            
    userNamelabel.font = UIFont.systemFontOfSize(homeTextSize)
        userNamelabel.textColor = UIColor.darkGrayColor()

        return userNamelabel
    }()
    
    //common_icon_membership_level1等级levelIconView
    private lazy var levelIconView: UIImageView = {
        let levelIconView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
        return levelIconView
    }()
    
    //时间
    private lazy var timeLabel: UILabel = {
        let timeLabel = UILabel()
        timeLabel.text = "time"
        timeLabel.textColor = UIColor.orangeColor()
        timeLabel.font = UIFont.systemFontOfSize(12)
        return timeLabel
    }()
    //来源
    private lazy var sourceLabel: UILabel = {
        let sourceLabel = UILabel()
        sourceLabel.text = "source"
        
        sourceLabel.font = UIFont.systemFontOfSize(12)
        sourceLabel.textColor = UIColor.grayColor()
        
        return sourceLabel
    }()
    //content内容
    private lazy var contentLabel: UILabel = {
        let contentLabel = UILabel()
        contentLabel.text = "contentLabel"
        contentLabel.numberOfLines = 0
        contentLabel.font = UIFont.systemFontOfSize(homeTextSize)
        contentLabel.textColor = UIColor.blackColor()
        contentLabel.sizeToFit()
        return contentLabel
    }()
}
